#ifndef YHAIDA_CURRENTOFTHREAD_H
#define YHAIDA_CURRENTOFTHREAD_H
#include <string>
#include <errno.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/prctl.h>
#include <sys/syscall.h>
#include <sys/types.h>
#include <linux/unistd.h>
#include "../datetime/Timestamp.h"
namespace yhaida
{
    namespace CurrentOfThread
    {
        // internal
        extern __thread int t_cachedTid;
        extern __thread char t_tidString[32];
        extern __thread int t_tidStringLength;
        extern __thread const char *t_threadName;
        void cacheTid();

        inline int tid()
        {
            if (__builtin_expect(t_cachedTid == 0, 0))
            {
                cacheTid();
            }
            return t_cachedTid;
        }

        inline const char *tidString() // for logging
        {
            return t_tidString;
        }

        inline int tidStringLength() // for logging
        {
            return t_tidStringLength;
        }

        inline const char *name()
        {
            return t_threadName;
        }

        bool isMainThread();

        void sleepUsec(int64_t usec); // for testing

        std::string stackTrace(bool demangle);
    }
}

#endif
